package com.tianwen.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianwen.management.entity.Department;
import com.tianwen.management.entity.Employee;
import com.tianwen.management.mapper.DepartmentMapper;
import com.tianwen.management.service.DepartmentService;
import com.tianwen.management.service.EmployeeService;
import com.tianwen.management.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.management.Query;
import java.nio.file.Watchable;
import java.sql.Wrapper;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【employee(员工表)】的数据库操作Service实现
* @createDate 2023-05-12 15:34:17
*/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{
@Resource
private EmployeeMapper employeeMapper;
@Resource
private DepartmentMapper departmentMapper;
    @Override
    public List<Employee> page(Integer page, Integer pageSize, String name) {
        QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        queryWrapper.orderByDesc("id");
        Page<Employee> pageInfo=new Page<>(page,pageSize);
        Page<Employee> employeePage = employeeMapper.selectPage(pageInfo, queryWrapper);
        List<Employee> employees = employeePage.getRecords().stream().map(item -> {
            Department department = departmentMapper.selectById(item.getDeptId());
             if(department!=null){
                 item.setDeptName(department.getName());
             }else{
                 item.setDeptName("无");
             }
            return item;
        }).collect(Collectors.toList());
        return  employees;
    }
}




